package zelix.hack.hacks.hud;


import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.inventory.GuiInventory;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.inventory.Slot;
import zelix.Core;
import zelix.gui.clickguis.N3ro.Utils.RenderUtil;
import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.hack.hacks.ClickGui;
import zelix.managers.FontManager;
import zelix.managers.HackManager;
import zelix.utils.Wrapper;
import zelix.utils.hooks.visual.ColorUtils;
import zelix.utils.hooks.visual.Colors;
import zelix.utils.hooks.visual.GuiRenderUtils;
import zelix.utils.hooks.visual.RenderUtils;
import zelix.utils.hooks.visual.font.FontLoaders;
import zelix.utils.hooks.visual.font.render.GlStateManager;
import zelix.utils.tenacityutils.render.ColorUtil;
import zelix.value.*;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiChat;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderGameOverlayEvent.Text;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import static zelix.hack.hacks.xray.gui.GuiOverlay.XrayStr;

public class HUD extends Hack {

	public static Minecraft mc = Wrapper.INSTANCE.mc();
	public BooleanValue effects,SessionInfo,Inentory ,notification,MUSICPLAYER,ListRainBow,ListBackRainBow,OraginRainBow,ListBackVis,ListBoxAlpha;

	public ModeValue HUDmode;

	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
	public static FontManager font=new FontManager() ;
	public ColorUtils c = new ColorUtils();
	private ResourceLocation logo = new ResourceLocation("logo.png");
	int rainbowTick = 0;// 全局变量他修改不到

	NumberValue invx;
	NumberValue ListBackTick;
	NumberValue ListOnUP;
	NumberValue ListAlpha;


	Boolean create = false;

	NumberValue invy;
	public BufferedImage trayIcon;
	public SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
	public HUD() {
		super("HUD", HackCategory.VISUAL);
		this.setToggled(true);
		this.setShow(false);
		//font=new FontManager();
		effects = new BooleanValue("Effects", false);
		SessionInfo = new BooleanValue("SessionInfo" ,false);
		Inentory = new BooleanValue("Inentory",false);
		invx = new NumberValue("InvX", 10D, 0D, 100D);
		invy = new NumberValue("InvY", 20D, 0D, 100D);
		notification = new BooleanValue("Notification" , false);
		HUDmode = new ModeValue("Mode" , new Mode("Logo" , false) , new Mode("Normal" ,true) , new Mode("OTC" ,false));
		ListRainBow = new BooleanValue("New ListRainbow",false);
		ListBackRainBow = new BooleanValue("New ListBackRainBow",false);
		ListBackTick = new NumberValue("ListBackTick", 80D, -60D, 500D);
		ListOnUP = new NumberValue("ListOnUP", 1.5D, 1.5D, 10D);
		OraginRainBow = new BooleanValue("OraginRainBow",false);
		ListBackVis = new BooleanValue("ListBackVis",false);
		ListBoxAlpha = new BooleanValue("isListBoxAlpha",true);
		ListAlpha = new NumberValue("ListAlpha", 0.35D, 0.1D, 1D);
		this.addValue(effects ,invx , invy ,Inentory , notification,HUDmode,ListRainBow,ListBackRainBow,ListBackTick,ListOnUP,OraginRainBow,ListBackVis,ListBoxAlpha,ListAlpha);
	}

	@Override
	public void onEnable() {
		super.onEnable();
	}

	@Override
	public String getDescription() {
		return "Heads-Up Display.";
	}

	@Override
	public void onRenderGameOverlay(Text event) {
		//font=new FontManager();
		//font=new FontManager();
		int rainbowTickc = 0;

		if (notification.getValue()) {
			//Core.notificationManager.draw();
		}

		if (Inentory.getValue()){
			drawInventory();
		}

		if (HUDmode.getMode("Logo").isToggled()){
			RenderUtils.drawImage(logo,2,2,100,100);
		}else if (HUDmode.getMode("Normal").isToggled()){
			font.getFont("SFR 11").drawStringWithShadow("Z", 2, 2,
					new Color(c.rainbow().getRed(), c.rainbow().getGreen(), c.rainbow().getBlue()).getRGB());
			font.getFont("SFR 11").drawStringWithShadow("elix" + "(" + sdf.format(new Date()) + ")",
					font.getFont("SFR 11").getStringWidth("Z") + 6, 2, 0xFFFFFFFF);
		}else if (HUDmode.getMode("OTC").isToggled()){
			String test = "AutoWidthTest";
			String serverip = mc.isSingleplayer() ? "singleplayer" : !mc.getCurrentServerData().serverIP.contains(":") ? mc.getCurrentServerData().serverIP + ":25565" : mc.getCurrentServerData().serverIP;
			String info = Core.NAME + " | " + Core.UN + " | " + Minecraft.getDebugFPS() + " fps | " + serverip + " | " + formatter.format(new Date());
			GuiRenderUtils.drawRect(5, 5, 	font.getFont("SFR 5").getWidth(info) + 4, 13, new Color(40, 40, 40));
			GuiRenderUtils.drawRoundedRect(5, 5, font.getFont("SFR 5").getWidth(info)  + 4 , 1, 1, new Color(255, 191, 0).getRGB(), 1, new Color(255, 191, 0).getRGB());
			font.getFont("SFR 5").drawStringWithShadow(info, 7, 7f, Colors.WHITE.c);
		}else if (HUDmode.getMode("GameSense").isToggled()){

		}



		ScaledResolution sr = new ScaledResolution(Wrapper.INSTANCE.mc());
		int index = invx.getValue().intValue();
		int index_right = invy.getValue().intValue();
		int DrawColor=0XFFFFFFFF;

		ArrayList<Hack> hacks = new ArrayList();
		hacks.addAll(HackManager.hacks);
		double x = Wrapper.INSTANCE.player().posX;
		double y = Wrapper.INSTANCE.player().posY;
		double z = Wrapper.INSTANCE.player().posZ;



		String coords = String.format("\u00a77X: \u00a7f%s \u00a77Y: \u00a7f%s \u00a77Z: \u00a7f%s",
				RenderUtils.DF((float) x, 1), RenderUtils.DF((float) y, 1), RenderUtils.DF((float) z, 1));


		boolean isChatOpen = Wrapper.INSTANCE.mc().currentScreen instanceof GuiChat;
		int heightCoords = isChatOpen ? sr.getScaledHeight() - 25 : sr.getScaledHeight() - 10;
		int colorRect = ColorUtils.color(0.0F, 0.0F, 0.0F, 0.0F);
		int colorRect2 = ColorUtils.color(0.0F, 0.0F, 0.0F, 0.5F);
		int heightFPS = isChatOpen ? sr.getScaledHeight() - 37 : sr.getScaledHeight() - 22;
		/*
		 * RenderUtils.drawStringWithRect(coords, 4, heightCoords, ClickGui.getColor(),
		 * colorRect, colorRect2);
		 *
		 * RenderUtils.drawStringWithRect("\u00a77FPS: \u00a7f"+Wrapper.INSTANCE.mc().
		 * getDebugFPS(), 4, heightFPS, ClickGui.getColor(), colorRect, colorRect2);
		 */

		if (!ClickGui.language.getMode("Chinese").isToggled()){
			for (Hack h : HackManager.getSortedHacks3()) {
				String modeName = "";
				if (!h.isToggled())
					continue;
				if (++rainbowTickc > 100) {
					rainbowTickc = 0;
				}
				if (h.getName().equals("Xray")){
					modeName =  modeName + " \u00a77" + XrayStr;
				}
				for (Value value : h.getValues()) {
					if (value instanceof ModeValue) {
						ModeValue modeValue = (ModeValue) value;
						if (modeValue.getModeName().equals("Mode")) {
							for (Mode mode : modeValue.getModes()) {
								if (mode.isToggled()) {
									modeName = modeName + " \u00a77" + mode.getName();
								}
							}
						}
					}
				}
				int yPos = 18;
				int xPos = 4;
				xPos = 6;
				//Gui.drawRect(xPos - 2, yPos - 2,(int)xPos+(int)sr.getScaledWidth() - (int)font.getFont("SFB 6").getWidth(h.getName() + modeName), yPos + 10, new Color(0,0,0,50).getRGB());
//			RenderUtils.drawStringWithRect("OKK", x, y, heightCoords, colorRect, colorRect2);

				Color rainbow = new Color(
						Color.HSBtoRGB((float) ((double) Minecraft.getMinecraft().player.ticksExisted / 50.0
								- Math.sin((double) rainbowTickc / 40.0 * 1.4+ 100)) % 1.0f, 0.5f, 0.8f));

				Color m_Backrainbow = new Color(
						Color.HSBtoRGB((float) ((double) Minecraft.getMinecraft().player.ticksExisted / 50.0
								- Math.sin((double) rainbowTickc / ListBackTick.getValue().doubleValue() * ListOnUP.getValue().doubleValue() + 100)) % 1.0f, 0.5f, 0.8f));

					if(ListRainBow.getValue().booleanValue()){
						DrawColor = rainbow.getRGB();
					}
					if(ListBackVis.getValue().booleanValue()){
						if(ListBoxAlpha.getValue().booleanValue()) {
							float alphaAnimation = 1;
							int SDrawColor = ColorUtil.applyOpacity(new Color(10, 10, 10), (float) (ListAlpha.getValue().doubleValue() * alphaAnimation)).getRGB();
							RenderUtil.drawBorderedRect(sr.getScaledWidth() - font.getFont("SFB 6").getWidth(h.getName() + modeName) - index_right - 4, index + 1.6, sr.getScaledWidth()
									- index_right, index + 13.6, 0, 0, SDrawColor);
						}
						else if(ListBackRainBow.getValue().booleanValue()){

								if(OraginRainBow.getValue().booleanValue()){
									RenderUtil.drawBorderedRect(sr.getScaledWidth() -font.getFont("SFB 6").getWidth(h.getName() + modeName)-index_right-4,index+1,sr.getScaledWidth()
											-index_right, index+13.6,1,ColorUtils.rainbow((int) (ListOnUP.getValue().doubleValue()*10)),ColorUtils.rainbow((int) (ListOnUP.getValue().doubleValue()*10)));
								}else{
									RenderUtil.drawBorderedRect(sr.getScaledWidth() -font.getFont("SFB 6").getWidth(h.getName() + modeName)-index_right-4,index+1,sr.getScaledWidth()
											-index_right, index+13.6,1,m_Backrainbow.getRGB(),m_Backrainbow.getRGB());
								}
						}else{
							RenderUtil.drawBorderedRect(sr.getScaledWidth() -font.getFont("SFB 6").getWidth(h.getName() + modeName)-index_right-4,index+1,sr.getScaledWidth()
									-index_right, index+13.6,1,0XFF1D1D1F,0XFF1D1D1F);
						}
					}


					font.getFont("SFB 6").drawString(h.getName() + modeName,
							sr.getScaledWidth() - font.getFont("SFB 6").getWidth(h.getName() + modeName)-index_right-2, index+2,
							DrawColor);

					index += font.getFont("SFB 6").getHeight(h.getName()) + 3;

			}
		}else{
			for (Hack h : HackManager.getSortedHacks()) {
				String modeName = "";
				if (!h.isToggled())
					continue;
				if (++rainbowTickc > 100) {
					rainbowTickc = 0;
				}
				if (h.getName().equals("Xray") && !(XrayStr.equals(""))){
					modeName =  modeName + " \u00a77" + XrayStr;
				}
				for (Value value : h.getValues()) {
					if (value instanceof ModeValue) {
						ModeValue modeValue = (ModeValue) value;
						if (modeValue.getModeName().equals("Mode")) {
							for (Mode mode : modeValue.getModes()) {
								if (mode.isToggled()) {
									modeName = modeName + " \u00a77" + mode.getName();

								}
							}
						}
					}
				}
				int yPos = 18;
				int xPos = 4;
				xPos = 6;
				//Gui.drawRect(xPos - 2, yPos - 2,(int)xPos+(int)sr.getScaledWidth() - (int)font.getFont("SFB 6").getWidth(h.getName() + modeName), yPos + 10, new Color(0,0,0,50).getRGB());
//			RenderUtils.drawStringWithRect("OKK", x, y, heightCoords, colorRect, colorRect2);
				Color rainbow = new Color(
						Color.HSBtoRGB((float) ((double) Minecraft.getMinecraft().player.ticksExisted / 50.0
								- Math.sin((double) rainbowTickc / 40.0 * 1.4+ 100)) % 1.0f, 0.5f, 0.8f));


				Color m_Backrainbow = new Color(
						Color.HSBtoRGB((float) ((double) Minecraft.getMinecraft().player.ticksExisted / 50.0
								- Math.sin((double) rainbowTickc / ListBackTick.getValue().doubleValue() * ListOnUP.getValue().doubleValue() + 100)) % 1.0f, 0.5f, 0.8f));

				if(ListRainBow.getValue().booleanValue()){
					DrawColor = rainbow.getRGB();
				}
				if(ListBackVis.getValue().booleanValue()){
					if(ListBoxAlpha.getValue().booleanValue()) {
						float alphaAnimation = 1;
						int SDrawColor = ColorUtil.applyOpacity(new Color(10, 10, 10), (float) (ListAlpha.getValue().doubleValue() * alphaAnimation)).getRGB();
						RenderUtil.drawBorderedRect(sr.getScaledWidth() - Wrapper.INSTANCE.fontRenderer().getStringWidth(h.getRenderName() + modeName) - index_right - 4, index + 1.6, sr.getScaledWidth()
								- index_right, index + 13.6, 0, 0, SDrawColor);
					}
					else if(ListBackRainBow.getValue().booleanValue()){
						if(OraginRainBow.getValue().booleanValue()){
							RenderUtil.drawBorderedRect(sr.getScaledWidth() -Wrapper.INSTANCE.fontRenderer().getStringWidth(h.getRenderName() + modeName)-index_right-4,index+1,sr.getScaledWidth()
									-index_right, index+13.6,1,ColorUtils.rainbow((int) (ListOnUP.getValue().doubleValue()*10)),ColorUtils.rainbow((int) (ListOnUP.getValue().doubleValue()*10)));
						}else{
							RenderUtil.drawBorderedRect(sr.getScaledWidth() -Wrapper.INSTANCE.fontRenderer().getStringWidth(h.getRenderName() + modeName)-index_right-4,index+1,sr.getScaledWidth()
									-index_right, index+13.6,1,m_Backrainbow.getRGB(),m_Backrainbow.getRGB());
						}

					}else{
						RenderUtil.drawBorderedRect(sr.getScaledWidth() -Wrapper.INSTANCE.fontRenderer().getStringWidth(h.getRenderName() + modeName)-index_right-4,index+1,sr.getScaledWidth()
								-index_right, index+13.6,1,0XFF1D1D1F,0XFF1D1D1F);
					}
				}

				Wrapper.INSTANCE.fontRenderer().drawString(h.getRenderName() + modeName,
						sr.getScaledWidth() -Wrapper.INSTANCE.fontRenderer().getStringWidth(h.getRenderName() + modeName)-index_right-2, index+2,
						DrawColor);


				index += Wrapper.INSTANCE.fontRenderer().FONT_HEIGHT + 3;

			}
		}

		super.onRenderGameOverlay(event);
	}



	public void drawInventory() {

		RenderHelper.enableGUIStandardItemLighting();
		GlStateManager.enableDepth();
		int x = 5;
		int y = 22;
		Gui.drawRect(x, y, x + 167, y + 73, new Color(29, 29, 29, 255).getRGB());
		Gui.drawRect(x + 1, y + 13, x + 166, y + 72, new Color(40, 40, 40, 255).getRGB());
		Core.fontLoaders.default16.drawString("Your Inventory", x + 3, y + 3, 0xffffffff, true);
		boolean hasStacks = false;
		for (int i1 = 9; i1 < Wrapper.INSTANCE.player().inventoryContainer.inventorySlots.size() - 9; ++i1) {
			Slot slot = Wrapper.INSTANCE.player().inventoryContainer.inventorySlots.get(i1);
			if (slot.getHasStack()) hasStacks = true;
			int i = slot.xPos;
			int j = slot.yPos;
			mc.getRenderItem().renderItemAndEffectIntoGUI(slot.getStack(), x + i - 4, y + j - 68);
			mc.getRenderItem().renderItemOverlayIntoGUI(Wrapper.INSTANCE.fontRenderer(), slot.getStack(), x + i - 4, y + j - 68, null);
		}
		if (mc.currentScreen instanceof GuiInventory) {
			FontLoaders.default16.drawString("Already in inventory",
					x + 167 / 2 - Core.fontLoaders.default16.getStringWidth("Already in inventory") / 2,
					y + 72 / 2,
					0xffffffff,
					true);
		} else if (!hasStacks) {
			Core.fontLoaders.default16.drawString("Empty...",
					x + 167 / 2 - Core.fontLoaders.default16.getStringWidth("Empty...") / 2,
					y + 72 / 2,
					0xffffffff,
					true);
		}
		RenderHelper.disableStandardItemLighting();
		GlStateManager.disableDepth();

	}
}